//
//
//
//
//  Created by lbxia on 15/10/21.
//  Copyright © 2015年 lbxia. All rights reserved.
//

#import "QQScanZXingViewController.h"
#import "LBXPermission.h"
#import "LBXPermissionSetting.h"
#import <HYVideoPlayer/HYVideoPlayerConfig.h>
#import "HYVideoViewModel.h"
#import "HYVideoRoomViewController.h"

@interface QQScanZXingViewController ()
@end

@implementation QQScanZXingViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)]) {
        
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
    self.view.backgroundColor = [UIColor blackColor];
    [self.view bringSubviewToFront:self.navigationBarView];
    
}

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    
    
    if (_topTitle) {
        
        _topTitle.bounds = CGRectMake(0, 0, 145, 60);
        _topTitle.center = CGPointMake(CGRectGetWidth(self.view.frame)/2, NAVIGATIONBAR_HEIGHT+50);
    }
    
    if (_bottomItemsView) {
        
        CGRect frame = CGRectMake(0, CGRectGetMaxY(self.view.bounds)-100,CGRectGetWidth(self.view.bounds), 100);

        
        self.bottomItemsView.frame = frame;
        CGSize size = CGSizeMake(65, 87);

        _btnFlash.bounds = CGRectMake(0, 0, size.width, size.height);
        _btnFlash.center = CGPointMake(CGRectGetWidth(_bottomItemsView.frame)* 3/4, CGRectGetHeight(_bottomItemsView.frame)/2);
        
        _btnPhoto.bounds = _btnFlash.bounds;
        _btnPhoto.center = CGPointMake(CGRectGetWidth(_bottomItemsView.frame)/ 4, CGRectGetHeight(_bottomItemsView.frame)/2);
    }
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
        
    [self drawBottomItems];
    [self drawTitle];
    [self.view bringSubviewToFront:_topTitle];
}

//绘制扫描区域
- (void)drawTitle
{
    if (!_topTitle)
    {
        self.topTitle = [[UILabel alloc]init];
        _topTitle.bounds = CGRectMake(0, 0, 200, 60);
        _topTitle.center = CGPointMake(CGRectGetWidth(self.view.frame)/2, 50);
        
        //3.5inch iphone
        if ([UIScreen mainScreen].bounds.size.height <= 568 )
        {
            _topTitle.center = CGPointMake(CGRectGetWidth(self.view.frame)/2, 38);
            _topTitle.font = [UIFont systemFontOfSize:14];
        }
        
        
        _topTitle.textAlignment = NSTextAlignmentCenter;
        _topTitle.numberOfLines = 0;
        _topTitle.text = NSLocalizedString(@"将取景框对准二维码即可自动扫描", nil);
        _topTitle.textColor = [UIColor whiteColor];
        [self.view addSubview:_topTitle];
    }    
}



- (void)drawBottomItems
{
    if (_bottomItemsView) {
        
        return;
    }
    
    CGRect frame = CGRectMake(0, CGRectGetMaxY(self.view.bounds)-100,CGRectGetWidth(self.view.bounds), 100);
    self.bottomItemsView = [[UIView alloc]initWithFrame:frame];
    _bottomItemsView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6];
    
    [self.view addSubview:_bottomItemsView];
    
    CGSize size = CGSizeMake(65, 87);
    self.btnFlash = [[UIButton alloc]init];
    _btnFlash.bounds = CGRectMake(0, 0, size.width, size.height);
    _btnFlash.center = CGPointMake(CGRectGetWidth(_bottomItemsView.frame)/2, CGRectGetHeight(_bottomItemsView.frame)/2);
     [_btnFlash setImage:[UIImage imageNamed:@"CodeScan.bundle/qrcode_scan_btn_flash_nor"] forState:UIControlStateNormal];
    [_btnFlash addTarget:self action:@selector(openOrCloseFlash) forControlEvents:UIControlEventTouchUpInside];
    
    self.btnPhoto = [[UIButton alloc]init];
    _btnPhoto.bounds = _btnFlash.bounds;
    _btnPhoto.center = CGPointMake(CGRectGetWidth(_bottomItemsView.frame)/4, CGRectGetHeight(_bottomItemsView.frame)/2);
    [_btnPhoto setImage:[UIImage imageNamed:@"CodeScan.bundle/qrcode_scan_btn_photo_nor"] forState:UIControlStateNormal];
    [_btnPhoto setImage:[UIImage imageNamed:@"CodeScan.bundle/qrcode_scan_btn_photo_down"] forState:UIControlStateHighlighted];
    [_btnPhoto addTarget:self action:@selector(openPhoto) forControlEvents:UIControlEventTouchUpInside];
    
    [_bottomItemsView addSubview:_btnFlash];
    [_bottomItemsView addSubview:_btnPhoto];
    
}




#pragma mark -底部功能项
//打开相册
- (void)openPhoto
{
    __weak __typeof(self) weakSelf = self;
    [LBXPermission authorizeWithType:LBXPermissionType_Photos completion:^(BOOL granted, BOOL firstTime) {
        if (granted) {
            [weakSelf openLocalPhoto:NO];
        }else if (!firstTime ){
            [LBXPermissionSetting showAlertToDislayPrivacySettingWithTitle:NSLocalizedString(@"提示", nil) msg:NSLocalizedString(@"没有开启相册权限，是否前往设置", nil) cancel:NSLocalizedString(@"取消", nil) setting:NSLocalizedString(@"设置", nil)];
        }
    }];
}

//开关闪光灯
- (void)openOrCloseFlash
{
    [super openOrCloseFlash];
   
    if (self.isOpenFlash)
    {
        [_btnFlash setImage:[UIImage imageNamed:@"CodeScan.bundle/qrcode_scan_btn_flash_down"] forState:UIControlStateNormal];
    }
    else
        [_btnFlash setImage:[UIImage imageNamed:@"CodeScan.bundle/qrcode_scan_btn_flash_nor"] forState:UIControlStateNormal];
}

- (void)scanResultWithArray:(NSArray<LBXScanResult*>*)array
{
    if(!array.count){
        
        [HYHub showMessage:NSLocalizedString(@"获取信息失败", nil)];
        // 重新扫码
        [self reStartDevice];
        return;
    }
    LBXScanResult *scanResult = array[0];
    NSString *strResult = scanResult.strScanned;
    if (!strResult) {
        [self reStartDevice];
        return;
    }
    [self.qRScanView stopScanAnimation];
    // 解码
    NSString *videoInfo = [strResult stringByRemovingPercentEncoding];
    NSArray *videoArray = [videoInfo componentsSeparatedByString:@"+"];
    NSString *videoTitle = videoArray.firstObject;
    NSString *videoUrl = videoArray.lastObject;
    CLog(@"videoTitle -- %@,videoUrl -- %@",videoTitle,videoUrl);
    [self pushVideoVcWithTitle:videoTitle videoUrl:videoUrl];
}

- (void)pushVideoVcWithTitle:(NSString *)videoTitle videoUrl:(NSString *)videoUrl
{
    NSString *title = [CommUtls fetchVideoNameWithFullName:videoTitle];
    HYVideoModel *videoModel = [[HYVideoModel alloc]init];
    videoModel.videoUrl = videoUrl;
    videoModel.videoTitle = videoTitle;
    videoModel.videoImagePath = [title stringByAppendingFormat:@".png"];
    videoModel.videoImage = [CommUtls fetchVideoCoverImageWithUrl:videoUrl atTime:1.0];
    NSString *createDate = [CommUtls encodeTimeYearMonthDay:[NSDate date]];
    videoModel.createDate = createDate;
    videoModel.isSelect = YES;
    HYVideoPlayerConfig *config = [HYVideoPlayerConfig new];
    config.isBasePlayer = YES;
    config.currentVideoModel = videoModel;
    config.videoSoucres = @[videoModel];
    HYVideoViewModel *viewModel = [[HYVideoViewModel alloc]initWithFolderIdentification:@"URL-vplayer" isPrivate:NO videoConfig:config];
    HYVideoRoomViewController *videoVC = [[HYVideoRoomViewController alloc]initWithViewModel:viewModel];
    [self.navigationController pushViewController:videoVC animated:YES];
    @weakify(self);
    [[videoVC rac_signalForSelector:@selector(viewDidAppear:)] subscribeNext:^(id x) {
        @strongify(self);
        NSMutableArray *viewControllers =[NSMutableArray arrayWithArray:self.navigationController.viewControllers];
        [viewControllers removeObject:self];
        self.navigationController.viewControllers = viewControllers;
    }];
}


@end
